package iot.core.data;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.util.StringUtils;

@Data
@Schema(name = "BasePageQto",description = "")
public class BasePageQto extends BaseQto {

    @Schema(name = "关键字",description = "")
    private  String  Key ="";

    @Schema(name = "请求页",description = "")
    private  Integer  Page =0;

    @Schema(name = "页数量",description = "")
    private  Integer  PageSize =0;

    @Schema(name = "排序",description = "")
    private  String  SortFields ="";


    public int getOffset(){
        return (getPage()-1)*getPageSize();
    }
    public  String getSortField(){
        var m=getSort();
        return  m.getFiled();
    }
    public  String getSortDir(){
        var m=getSort();
        return  m.isIsAsc()?"asc":"desc";
    }
    public Sort getSort()
    {
        if (!StringUtils.hasText(SortFields))
        {
            var r=new Sort();
            r.setFiled("Id");
            r.setIsAsc(true);
            return r;
        }
        var arr = SortFields.split( ",");
        if(arr.length== 0)
        {
            var r=new Sort();
            r.setFiled("Id");
            r.setIsAsc(true);
            return r;
        }
        var arr2 = arr[0].split( " ");
        if (arr2.length !=2)
        {
            var r=new Sort();
            r.setFiled("Id");
            r.setIsAsc(true);
            return r;
        }

        {
            arr2[1] = arr2[1].toLowerCase();
            var r=new Sort();
            r.setFiled(arr2[0].trim() );
            r.setIsAsc( "asc".equals(arr2[1]) );
            return r;

        }
    }
}
